package com.aos.eap.activity;

import android.widget.LinearLayout;
import android.widget.TextView;

import com.aos.eap.R;
import com.aos.eap.adapter.SearchEqpListAdapter;
import com.aos.eap.base.BaseDialogActivity;
import com.aos.eap.base.Constant;
import com.aos.eap.bean.SearchEqpBean;
import com.aos.eap.bean.net.BaseResponseBean;
import com.aos.eap.utils.MyUtils;
import com.aos.eap.utils.NetUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class SearchEqpActivity extends BaseDialogActivity {


    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvEqpId)
    TextView tvEqpId;
    @BindView(R.id.tvEqpType)
    TextView tvEqpType;
    @BindView(R.id.tvMesStatus)
    TextView tvMesStatus;
    @BindView(R.id.tvControlStatus)
    TextView tvControlStatus;
    @BindView(R.id.tvRunStatus)
    TextView tvRunStatus;
    @BindView(R.id.rvEqpInfo)
    RecyclerView rvEqpInfo;
    @BindView(R.id.tvLotId)
    TextView tvLotId;
    @BindView(R.id.tvLotNum)
    TextView tvLotNum;
    @BindView(R.id.tvHanxianId)
    TextView tvHanxianId;
    @BindView(R.id.tvRecipeId)
    TextView tvRecipeId;
    @BindView(R.id.tvBdId)
    TextView tvBdId;
    @BindView(R.id.tvPidaoLotId)
    TextView tvPidaoLotId;
    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.tvUser)
    TextView tvUser;

    private SearchEqpListAdapter searchEqpListAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_search_eqp;
    }

    @Override
    protected void initView() {
        tvUser.setText(Constant.CURRENT_USER);
        rvEqpInfo.setLayoutManager(new LinearLayoutManager(SearchEqpActivity.this));
        rvEqpInfo.setItemAnimator(new DefaultItemAnimator());
        rvEqpInfo.addItemDecoration(new DividerItemDecoration(SearchEqpActivity.this, 1));
        searchEqpListAdapter = new SearchEqpListAdapter(null);
        searchEqpListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        searchEqpListAdapter.isFirstOnly(true);
        rvEqpInfo.setAdapter(searchEqpListAdapter);
    }

    @Override
    protected void initData() {
        MyUtils.setEditTextListener(etEqpId, this, this::requestInfoByEqpId);
    }

    private void requestInfoByEqpId(String eqpId) {
        JsonObject map = new JsonObject();
        map.addProperty("EqpID", eqpId);
        NetUtils.requestGetNet(this, "EMS_EQPEARCH", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<SearchEqpBean>>(
                new TypeToken<BaseResponseBean<SearchEqpBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<SearchEqpBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    setPageData(response.body().getResponse().getBody());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<SearchEqpBean>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_EQPEARCH");
            }
        });
    }

    private void setPageData(SearchEqpBean searchEqpBean) {
        tvEqpId.setText(searchEqpBean.getEqphead().getEqpid());
        tvEqpType.setText(searchEqpBean.getEqphead().getEqptype());
        tvMesStatus.setText(searchEqpBean.getEqphead().getMesstate());
        tvControlStatus.setText(searchEqpBean.getEqphead().getControlstate());
        tvRunStatus.setText(searchEqpBean.getEqphead().getRunstate());
        tvLotId.setText(searchEqpBean.getEqphead().getLotname());
        tvLotNum.setText(searchEqpBean.getEqphead().getLotcount());
        tvHanxianId.setText(searchEqpBean.getEqphead().getWirenum());
        tvRecipeId.setText(searchEqpBean.getEqphead().getRecipename());
        tvBdId.setText(searchEqpBean.getEqphead().getBdnum());
        tvPidaoLotId.setText(searchEqpBean.getEqphead().getCaplotnum());
        searchEqpListAdapter.setNewData(searchEqpBean.getEqpbodies());
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("设备信息查询");
        freeUI_titleView.setLeftListener(view -> showBackDialog(SearchEqpActivity.this));
        freeUI_titleView.setRightListener(R.drawable.refresh, v -> {
            if (MyUtils.isFastClick()){
                showToast("您点击的太快了",false);
                return;
            }
            String eqpId = etEqpId.getText().toString().trim();
            if (FreeApi_StringUtils.isEmpty(eqpId)) {
                showToast("请扫描设备号", false);
                return;
            }
            requestInfoByEqpId(eqpId);
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }
}
